package com.zr.service.car.impl;

import com.github.pagehelper.PageHelper;
import com.zr.mapper.car.CarMapper;
import com.zr.service.car.CarService;
import com.zr.util.TokenUtil;
import com.zr.vo.car.Car;
import com.zr.vo.sys.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class CarServiceImpl implements CarService {

    private static final String HEAD_FILE_DIR = "F:\\";
    @Resource
    private CarMapper carMapper;

    @Autowired
    private TokenUtil tokenUtil;

    /**
     * 查询车辆
     * @param car
     * @return
     */
    @Override
    public List<Car> list(Car car) {
        PageHelper.startPage(car.getPageNum(),car.getPageSize());
        return carMapper.list(car);
    }

    /**
     * 添加车辆
     * @param car
     * @return
     */
    @Transactional
    @Override
    public void add(Car car) {
        car.setCreateId(tokenUtil.getUserId());
        car.setCreateTime(new Date());
        carMapper.insertSelective(car);

    }

    /**
     * 编辑车辆
     * @param car
     */
    @Transactional
    @Override
    public void edit(Car car) {
        car.setModifyId(tokenUtil.getUserId());
        car.setModifyTime(new Date());
        carMapper.updateByPrimaryKeySelective(car);
    }

    /**
     * 删除车辆
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        carMapper.deleteByPrimaryKey(id);
    }

    /**
     * 图片上传
     * @return String
     */
    @Override
    public void uploadImg(MultipartFile file, Long id) throws IOException {
        String extensionName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String path = HEAD_FILE_DIR + UUID.randomUUID().toString() + extensionName;
        File imgFile = new File(path);
        file.transferTo(imgFile);
        Car car = carMapper.selectByPrimaryKey(id);
        car.setImgUrl(path);
        carMapper.updateByPrimaryKeySelective(car);

    }

    /**
     * 图片下载
     * @return String
     */
    @Override
    public String loadImg(Long id) {
        Car car = carMapper.selectByPrimaryKey(id);
        return loadImgByImgUrl(car.getImgUrl());
    }

    /**
     * 根据文件地址生成base64编码
     * @param imgUrl
     * @return
     */
    @Override
    public String loadImgByImgUrl(String imgUrl) {
        if(imgUrl == null||imgUrl.equals("")) return "";
        byte[] b = new byte[0];
        File file = new File(imgUrl);
        try(FileInputStream fis = new FileInputStream(file)){
            b =  new byte[(int)file.length()];
            fis.read(b);
        }catch (Exception e){}
        return Base64.getEncoder().encodeToString(b);
    }

    /**
     *
     * @return String
     */
    @Override
    public String image(Long id){
        Car car = carMapper.selectByPrimaryKey(id);
        return loadImgByImgUrl(car.getImgUrl());
    }


}
